/**
 * @param {number[][]} grid
 * @return {number}
 */
var closedIsland = function (grid) {
  let count = 0, len1 = grid.length, len2 = grid[0].length
  function dfs(row, col) {
    if (row < 0 || row >= len1 || col < 0 || col >= len2) return false
    if (grid[row][col] != 0) return true
    grid[row][col] = -1
    return dfs(row + 1, col) && dfs(row - 1, col) && dfs(row, col + 1) && dfs(row, col - 1)

  }
  for (let i = 0; i < len1; i++) {
    for (let j = 0; j < len2; j++) {
      if (grid[i][j] == 0 && dfs(i, j)) {
        count++
      }
    }
  }

  return count
};